using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;


namespace ASPNETVN.PORTAL.Modules.EX.Components
{
    public partial class Item
    {
        #region Private members.

        protected string _CurrencyCode = string.Empty;
        protected DateTime _FromDate = new DateTime(1753, 1, 1);
        protected DateTime _ToDate = new DateTime(1753, 1, 1);
        protected string _CurrencyName = string.Empty;
        protected decimal _ExchangeRate;
        protected DateTime _CreatedDate = new DateTime(1753, 1, 1);
        protected string _CreatedBy = string.Empty;

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Properties.

        public string CurrencyCode
        {
            set { this._CurrencyCode = value; }
            get { return this._CurrencyCode; }
        }

        public DateTime FromDate
        {
            set { this._FromDate = value; }
            get { return this._FromDate; }
        }

        public DateTime ToDate
        {
            set { this._ToDate = value; }
            get { return this._ToDate; }
        }

        public string CurrencyName
        {
            set { this._CurrencyName = value; }
            get { return this._CurrencyName; }
        }

        public decimal ExchangeRate
        {
            set { this._ExchangeRate = value; }
            get { return this._ExchangeRate; }
        }

        public DateTime CreatedDate
        {
            set { this._CreatedDate = value; }
            get { return this._CreatedDate; }
        }

        public string CreatedBy
        {
            set { this._CreatedBy = value; }
            get { return this._CreatedBy; }
        }


        #endregion

        //---------------------------------------------------------------------------------------------

        #region Select methods.

        public static Item Load(string currencyCode, DateTime fromDate, DateTime toDate)
        {
            const string spName = "[dbo].[p_EX_Item_Load]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@CurrencyCode", SqlDbType.Char, currencyCode);
            db.AddInParameter(dbCommand, "@FromDate", SqlDbType.DateTime, fromDate);
            db.AddInParameter(dbCommand, "@ToDate", SqlDbType.DateTime, toDate);
            Item entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new Item();
                if (!reader.IsDBNull(reader.GetOrdinal("CurrencyCode"))) entity.CurrencyCode = reader.GetString(reader.GetOrdinal("CurrencyCode"));
                if (!reader.IsDBNull(reader.GetOrdinal("FromDate"))) entity.FromDate = reader.GetDateTime(reader.GetOrdinal("FromDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("ToDate"))) entity.ToDate = reader.GetDateTime(reader.GetOrdinal("ToDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("CurrencyName"))) entity.CurrencyName = reader.GetString(reader.GetOrdinal("CurrencyName"));
                if (!reader.IsDBNull(reader.GetOrdinal("ExchangeRate"))) entity.ExchangeRate = reader.GetDecimal(reader.GetOrdinal("ExchangeRate"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
            }
            reader.Close();
            return entity;
        }

        //---------------------------------------------------------------------------------------------
        public static List<Item> SelectCollectionAll()
        {
            List<Item> collection = new List<Item>();
            SqlDataReader reader = (SqlDataReader)SelectReaderAll();
            while (reader.Read())
            {
                Item entity = new Item();

                if (!reader.IsDBNull(reader.GetOrdinal("CurrencyCode"))) entity.CurrencyCode = reader.GetString(reader.GetOrdinal("CurrencyCode"));
                if (!reader.IsDBNull(reader.GetOrdinal("FromDate"))) entity.FromDate = reader.GetDateTime(reader.GetOrdinal("FromDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("ToDate"))) entity.ToDate = reader.GetDateTime(reader.GetOrdinal("ToDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("CurrencyName"))) entity.CurrencyName = reader.GetString(reader.GetOrdinal("CurrencyName"));
                if (!reader.IsDBNull(reader.GetOrdinal("ExchangeRate"))) entity.ExchangeRate = reader.GetDecimal(reader.GetOrdinal("ExchangeRate"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
                collection.Add(entity);
            }

            reader.Close();
            return collection;
        }

        //---------------------------------------------------------------------------------------------

        public static List<Item> SelectCollectionDynamic(string whereCondition, string orderByExpression)
        {
            List<Item> collection = new List<Item>();

            SqlDataReader reader = (SqlDataReader)SelectReaderDynamic(whereCondition, orderByExpression);
            while (reader.Read())
            {
                Item entity = new Item();

                if (!reader.IsDBNull(reader.GetOrdinal("CurrencyCode"))) entity.CurrencyCode = reader.GetString(reader.GetOrdinal("CurrencyCode"));
                if (!reader.IsDBNull(reader.GetOrdinal("FromDate"))) entity.FromDate = reader.GetDateTime(reader.GetOrdinal("FromDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("ToDate"))) entity.ToDate = reader.GetDateTime(reader.GetOrdinal("ToDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("CurrencyName"))) entity.CurrencyName = reader.GetString(reader.GetOrdinal("CurrencyName"));
                if (!reader.IsDBNull(reader.GetOrdinal("ExchangeRate"))) entity.ExchangeRate = reader.GetDecimal(reader.GetOrdinal("ExchangeRate"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
                collection.Add(entity);
            }

            reader.Close();
            return collection;
        }

        //---------------------------------------------------------------------------------------------

        // Select by foreign key return collection		


        public static DataSet SelectAll()
        {
            const string spName = "[dbo].[p_EX_Item_SelectAll]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
        {
            const string spName = "[dbo].[p_EX_Item_SelectDynamic]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderAll()
        {
            const string spName = "[dbo].[p_EX_Item_SelectAll]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
        {
            const string spName = "[dbo].[p_EX_Item_SelectDynamic]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        // Select by foreign key return collection		

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert methods.

        public static int InsertItem(string currencyCode, DateTime fromDate, DateTime toDate, string currencyName, decimal exchangeRate, string createdBy)
        {
            Item entity = new Item();
            entity.CurrencyCode = currencyCode;
            entity.FromDate = fromDate;
            entity.ToDate = toDate;
            entity.CurrencyName = currencyName;
            entity.ExchangeRate = exchangeRate;
            entity.CreatedBy = createdBy;
            return entity.Insert();
        }

        public int Insert()
        {
            return this.Insert(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Insert(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_EX_Item_Insert]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@CurrencyCode", SqlDbType.Char, CurrencyCode);
            db.AddInParameter(dbCommand, "@FromDate", SqlDbType.DateTime, FromDate.Year <= 1753 ? DBNull.Value : (object)FromDate);
            db.AddInParameter(dbCommand, "@ToDate", SqlDbType.DateTime, ToDate.Year <= 1753 ? DBNull.Value : (object)ToDate);
            db.AddInParameter(dbCommand, "@CurrencyName", SqlDbType.NVarChar, CurrencyName);
            db.AddInParameter(dbCommand, "@ExchangeRate", SqlDbType.Money, ExchangeRate);
            db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.VarChar, CreatedBy);

            if (transaction != null)
            {
                return db.ExecuteNonQuery(dbCommand, transaction);
            }
            else
            {
                return db.ExecuteNonQuery(dbCommand);
            }
        }

        //---------------------------------------------------------------------------------------------
        public static bool InsertCollection(IList<Item> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (Item item in collection)
                        {
                            if (item.Insert(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at InsertCollection method: " + ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert / Update methods.

        public static int InsertUpdateItem(string currencyCode, DateTime fromDate, DateTime toDate, string currencyName, decimal exchangeRate, DateTime createdDate, string createdBy)
        {
            Item entity = new Item();
            entity.CurrencyCode = currencyCode;
            entity.FromDate = fromDate;
            entity.ToDate = toDate;
            entity.CurrencyName = currencyName;
            entity.ExchangeRate = exchangeRate;
            entity.CreatedBy = createdBy;
            return entity.InsertUpdate();
        }

        //---------------------------------------------------------------------------------------------

        public int InsertUpdate()
        {
            return this.InsertUpdate(null);
        }

        //---------------------------------------------------------------------------------------------

        public int InsertUpdate(SqlTransaction transaction)
        {
            const string spName = "p_EX_Item_InsertUpdate";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@CurrencyCode", SqlDbType.Char, CurrencyCode);
            db.AddInParameter(dbCommand, "@FromDate", SqlDbType.DateTime, FromDate.Year == 1753 ? DBNull.Value : (object)FromDate);
            db.AddInParameter(dbCommand, "@ToDate", SqlDbType.DateTime, ToDate.Year == 1753 ? DBNull.Value : (object)ToDate);
            db.AddInParameter(dbCommand, "@CurrencyName", SqlDbType.NVarChar, CurrencyName);
            db.AddInParameter(dbCommand, "@ExchangeRate", SqlDbType.Money, ExchangeRate);
            db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.VarChar, CreatedBy);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------
        public static bool InsertUpdateCollection(IList<Item> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (Item item in collection)
                        {
                            if (item.InsertUpdate(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at InsertUpdateCollection method: " + ex.Message);

                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }
        #endregion

        //---------------------------------------------------------------------------------------------

        #region Update methods.

        public static int UpdateItem(string currencyCode, DateTime fromDate, DateTime toDate, string currencyName, decimal exchangeRate, string createdBy)
        {
            Item entity = new Item();
            entity.CurrencyCode = currencyCode;
            entity.FromDate = fromDate;
            entity.ToDate = toDate;
            entity.CurrencyName = currencyName;
            entity.ExchangeRate = exchangeRate;
            entity.CreatedBy = createdBy;
            return entity.Update();
        }

        //---------------------------------------------------------------------------------------------

        public int Update()
        {
            return this.Update(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Update(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_EX_Item_Update]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@CurrencyCode", SqlDbType.Char, CurrencyCode);
            db.AddInParameter(dbCommand, "@FromDate", SqlDbType.DateTime, FromDate.Year == 1753 ? DBNull.Value : (object)FromDate);
            db.AddInParameter(dbCommand, "@ToDate", SqlDbType.DateTime, ToDate.Year == 1753 ? DBNull.Value : (object)ToDate);
            db.AddInParameter(dbCommand, "@CurrencyName", SqlDbType.NVarChar, CurrencyName);
            db.AddInParameter(dbCommand, "@ExchangeRate", SqlDbType.Money, ExchangeRate);
            db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.VarChar, CreatedBy);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------
        public static bool UpdateCollection(IList<Item> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (Item item in collection)
                        {
                            if (item.Update(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at UpdateCollection method: " + ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Delete methods.

        public static int DeleteItem(string currencyCode, DateTime fromDate, DateTime toDate)
        {
            Item entity = new Item();
            entity.CurrencyCode = currencyCode;
            entity.FromDate = fromDate;
            entity.ToDate = toDate;

            return entity.Delete();
        }

        public int Delete()
        {
            return this.Delete(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Delete(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_EX_Item_Delete]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@CurrencyCode", SqlDbType.Char, CurrencyCode);
            db.AddInParameter(dbCommand, "@FromDate", SqlDbType.DateTime, FromDate);
            db.AddInParameter(dbCommand, "@ToDate", SqlDbType.DateTime, ToDate);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static bool DeleteCollection(IList<Item> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (Item item in collection)
                        {
                            if (item.Delete(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at DeleteCollection method: " + ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }
        #endregion
    }
}